package dao

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"time"
	"website/utils"
)

var Easy *gorm.DB

func InitDB() (err error) {
	// 初始化mysql连接
	m := utils.MysqlConfInit()
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", m.User, m.Passwd, m.Host, m.Port, m.DB)
	Easy, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	db, err := Easy.DB()
	if err != nil {
		return err
	}
	db.SetMaxIdleConns(m.MaxIdleCons)
	db.SetMaxOpenConns(m.MaxOpenCons)
	db.SetConnMaxLifetime(time.Hour)
	return
}
